<?php
$HOST="192.168.1.87";
$PORT=3201;
#$HOST="124.42.127.82";
#$PORT=3010;

function get_socket( $host, $port )
{
    global $HOST;
    global $PORT;
    #printf("HOST = %s, PORT = %d\n", $HOST, $PORT );
    printf("HOST = %s, PORT = %d\n", $host, $port );
    $sock = socket_create( AF_INET, SOCK_STREAM, SOL_TCP );
    socket_set_option( $sock, SOL_SOCKET, SO_LINGER, array("l_onoff"=>1, "l_linger"=>0) );

    $res = socket_connect( $sock, $host, $port );
    if( false == $res ) {
        printf("==%s",socket_strerror( socket_last_error( $sock ) ) );
        return false;
    }
    return $sock;
}
function get_news_request( $newsID )
{
    $newsDate = "2010.01.16";
    $newsTitle = "title";
    $newsPath = "/home/liujikun/news";
    $newsUrl = "http://www.baidu.com";
    $newsText = "hello, this is new formal text.";
    $totalLen = 512 + strlen($newsText);

    printf("============ packet content =============\n");
    printf("\t\ttotalLen = %d\n", $totalLen );
    printf("\t\tnewsID = %d\n", $newsID );
    printf("\t\tnewsDate = %s\n", $newsDate );
    printf("\t\tnewsTitle = %s\n", $newsTitle );
    printf("\t\tnewsPath = %s\n", $newsPath );
    printf("\t\tnewsUrl = %s\n", $newsUrl );
    printf("\t\tnewsText = %s\n", $newsText );
    $request = pack('NNNNa32a80a128a256'
        , $totalLen
        , 0x00005000
        , 0x00000011
        , $newsID
        , $newsDate
        , $newsTitle
        , $newsPath
        , $newsUrl);
    $request .= $newsText;
    return $request;
    #printf("len = %d\n", strlen($request) );

}
?>
